//
//  PMShape.h
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 10/29/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "PMCore.h"

@class PMBody;
@class PMShape;


#pragma mark Constants / Enums

// Collision Type
typedef cpCollisionType PMCollisionType;

// Collision Group
typedef cpGroup PMCollisionGroup;
static const PMCollisionGroup PMCollisionGroupNone = 0;

// Collision Layer Mask
typedef cpLayers PMLayerMask;
static const PMLayerMask PMLayerMaskAll = 0xFFFF;
static const PMLayerMask PMLayerMaskNone = 0x0000;

// Shape Types
typedef enum _PMShapeType {
	PMShapeTypeCircle,
	PMShapeTypeSegment,
	PMShapeTypePolygon,
} PMShapeType;

// Segment Query Info
typedef struct _PMSegmentCollisionInfo {
	PMSegment segment;		// the colliding segment
	PMShape *shape;		// shape that was hit; nil if no collision
	PMFloat intersect;	// distance of intersection point along segment. (range [0,1])  
	PMVect normal;		// surface normal of collided surface.
} PMSegmentCollisionInfo;

// Segment Query constant for no collision (for fast checking)
static const PMSegmentCollisionInfo PMSegmentCollisionInfoNone = 
{
	{{0.0, 0.0}, {0.0, 0.0}},
	nil, 
	0.0, 
	{0.0, 0.0}
};
	

#pragma mark -


@interface PMShape : NSObject {
@protected
	// Chipmunk collision shape reference
	cpShape *shape;
	
@private
	// Corresponding PMBody object affected by collisions
	PMBody *body;
	
	// Direction in which shapes may pass through this shape
	PMVect passThrough;
	
	// User defined data pointer
	id data;
}

@property (nonatomic, retain) PMBody *body;
@property (assign) PMVect passThrough;
@property (nonatomic, retain) id data;

// Property definitions of getters/setters for data members stored by backend 
// Chipmunk structure. These properties are not synthesized, because their 
// implementations are given explicitly.
@property (assign, readonly) cpShape *cpShape;
@property (assign, readonly) PMShapeType type;
@property (assign, readonly) PMBoundingBox boundingBox;
@property (assign) PMFloat elasticity;
@property (assign) PMFloat friction;
@property (assign) PMVect surfaceVelocity;
@property (assign) PMCollisionType collisionType;
@property (assign) PMCollisionType collisionGroup;
@property (assign) PMLayerMask layers;
@property (assign, readonly) NSUInteger hashID;
@property (assign, getter=isSensor) BOOL sensor;


#pragma mark -
#pragma mark Other Methods
- (BOOL)containsPoint:(PMVect)point;
- (BOOL)containsPoint:(PMVect)point layers:(PMLayerMask)layers group:(PMCollisionGroup)group;
- (BOOL)intersectsSegment:(PMSegment)segment layers:(PMLayerMask)layers group:(PMCollisionGroup)group info:(PMSegmentCollisionInfo *)info;
+ (void)resetShapeIDCounter;

@end


#pragma mark -
#pragma mark Functions
PMVect PMSegmentCollisionHitPoint(PMSegmentCollisionInfo info);
PMFloat PMSegmentCollisionHitDistance(PMSegmentCollisionInfo info);
